//
// Created by afly on 20-12-4.
// Mail: a_fly0505@163.com
// QQ: 785230837 WX: afly003
// Company: juefx
//

#ifndef __MY_SEMPHORE_HPP__
#define __MY_SEMPHORE_HPP__

#include <chrono>
#include <mutex>
#include <atomic>
#include <condition_variable>
#include "noncopyable.h"

namespace jfx {

#define  EVENTS_FAIL      (-1)
#define  EVENTS_SUCCESS   0
#define  EVENTS_WAIT      1
#define  EVENTS_TIME_OUT  2

    class EventsWait : public noncopyable
    {
    public:
        explicit EventsWait(const int sigs) : m_signals(sigs){}
        ~EventsWait() {}

    private:
        std::mutex m_mtx;
        std::condition_variable m_cv;
        std::atomic<int> m_signals;

    public:
        int Wait(const int time_out_ms = (-1));
        void Signal(const int count = 1);
        void Add(const int count = 1);


        inline int32_t GetCount(){
            return m_signals.load();
        }

        inline void Reset(int n) {
            m_signals.store(n);
        }
    };
}

#endif
